"""
基础数据模块序列化器
"""
from rest_framework import serializers
from django.contrib.auth.models import User
from .models import College, Major, Classes, Course, Punish, Score, PunishDict


class CollegeSerializer(serializers.ModelSerializer):
    """学院序列化器"""
    class Meta:
        model = College
        fields = ['id', 'college_name']
        read_only_fields = ['id']


class MajorSerializer(serializers.ModelSerializer):
    """专业序列化器"""
    college_name = serializers.CharField(source='college.college_name', read_only=True)
    
    class Meta:
        model = Major
        fields = ['id', 'major_name', 'college', 'college_name']
        read_only_fields = ['id']


class ClassesSerializer(serializers.ModelSerializer):
    """班级序列化器"""
    teacher_name = serializers.CharField(source='teacher.username', read_only=True)
    major_name = serializers.CharField(source='major.major_name', read_only=True)
    
    class Meta:
        model = Classes
        fields = ['id', 'class_name', 'teacher', 'teacher_name', 'major', 'major_name']
        read_only_fields = ['id']


class CourseSerializer(serializers.ModelSerializer):
    """课程序列化器"""
    major_name = serializers.CharField(source='major.major_name', read_only=True)
    
    class Meta:
        model = Course
        fields = ['id', 'course_name', 'major', 'major_name', 'teacher_name']
        read_only_fields = ['id']


class PunishSerializer(serializers.ModelSerializer):
    """处分序列化器"""
    teacher_name = serializers.CharField(source='teacher.username', read_only=True)
    student_name = serializers.CharField(source='student.username', read_only=True)
    
    class Meta:
        model = Punish
        fields = [
            'id', 'sort', 'name', 'ranks', 'grade',
            'teacher', 'teacher_name', 'student', 'student_name',
            'time', 'created_at'
        ]
        read_only_fields = ['id', 'created_at']


class ScoreSerializer(serializers.ModelSerializer):
    """综测分序列化器"""
    student_id = serializers.SerializerMethodField(read_only=True)
    student_name = serializers.SerializerMethodField(read_only=True)
    class_name = serializers.SerializerMethodField(read_only=True)
    major = serializers.SerializerMethodField(read_only=True)
    
    class Meta:
        model = Score
        fields = [
            'id', 'student', 'student_name', 'student_id', 'class_name', 'major',
            'time', 'grade', 'zb_grade', 'db_grade', 'tb_grade',
            'za_grade', 'da_grade', 'ta_grade', 'fail',
            'created_at', 'updated_at'
        ]
        read_only_fields = ['id', 'created_at', 'updated_at']
    
    def get_student_id(self, obj):
        """获取学号（优先从Profle获取，若无则回退到username）"""
        if obj.student:
            if hasattr(obj.student, 'profile') and obj.student.profile and obj.student.profile.student_id:
                return obj.student.profile.student_id
            return obj.student.username
        return None
    
    def get_student_name(self, obj):
        """获取学生姓名（优先返回first_name，若无则返回username）"""
        if obj.student:
            return obj.student.first_name or obj.student.username
        return None
    
    def get_class_name(self, obj):
        """获取班级名称"""
        if obj.student and hasattr(obj.student, 'profile') and obj.student.profile:
            return obj.student.profile.class_name or '-'
        return '-'
    
    def get_major(self, obj):
        """获取专业名称"""
        if obj.student and hasattr(obj.student, 'profile') and obj.student.profile:
            return obj.student.profile.major or '-'
        return '-'


class PunishDictSerializer(serializers.ModelSerializer):
    """处分字典序列化器"""
    class Meta:
        model = PunishDict
        fields = ['id', 'sort', 'name', 'ranks', 'grade']
        read_only_fields = ['id']
